package com.general.template.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.general.template.dto.QuestionCommonDto;
import com.general.template.dto.QuestionPersonalityDto;
import com.general.template.mapper.PowerQuestionCommonMapper;
import com.general.template.pojo.PowerDimension;
import com.general.template.pojo.PowerQuestionCommon;
import com.general.template.utils.IdWorker;
import com.general.template.utils.Pages;
import com.general.template.utils.StringUtils;
import com.general.template.vo.PersonalityRandomVo;
import com.general.template.vo.QuestionCommonVo;
import com.general.template.vo.QuestionPersonalityVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.general.template.pojo.PowerQuestionPersonality;
import com.general.template.mapper.PowerQuestionPersonalityMapper;
import com.general.template.service.PowerQuestionPersonalityService;
/**
 * @Author: 杰哥
 * Date: 2021/2/17 16:10
 * @Description:
**/

@Service
public class PowerQuestionPersonalityServiceImpl extends ServiceImpl<PowerQuestionPersonalityMapper, PowerQuestionPersonality> implements PowerQuestionPersonalityService{

    @Autowired
    private IdWorker worker;

    @Override
    public int addQuestion(PowerQuestionPersonality question) {
        PowerQuestionPersonalityMapper mapper = getBaseMapper();
        return mapper.insert(question);
    }

    @Override
    public Map<String, Object> addList(List<QuestionPersonalityVo> vo) {
        PowerQuestionPersonalityMapper mapper = getBaseMapper();
        Map<String, Object> map = new HashMap<>(16);
        long success = 0;
        long fail = 0;
        for (QuestionPersonalityVo personVo : vo) {
            if (StringUtils.isNullOrEmpty(personVo.getQuestionContent())||StringUtils.isNullOrEmpty(personVo.getQuestionOptions())){
                continue;
            }
            PowerQuestionPersonality personality = new PowerQuestionPersonality();
            personality.setQuestionType("单选题");
            personality.setId(Long.valueOf(worker.nextId()).toString());
            personality.setQuestionContent(personVo.getQuestionContent());
            personality.setInspectionName(personVo.getInspectionName());
            personality.setQuestionMediaUrl(mapToJson(personVo.getQuestionMediaName()));
            personality.setQuestionMediaName(mapToJson(personVo.getQuestionMediaName()));
            personality.setQuestionMediaStatus(personVo.getQuestionMediaStatus());
            personality.setQuestionOptions(mapToJson(personVo.getQuestionOptions()));
            personality.setQuestionOptionScore(mapToJson(personVo.getQuestionOptionScore()));
            personality.setJobsId(personVo.getJobsId());
            personality.setCreateTime(new Date());
            personality.setDimensionId(personVo.getDimensionId());
            int flat = addQuestion(personality);
            if (flat == 0) {
                fail++;
            } else {
                success++;
            }
        }
        map.put("success", success);
        map.put("fail", fail);
        return map;
    }

    @Override
    public Pages<QuestionPersonalityDto<String>> pageList(Pages<QuestionPersonalityDto<String>> pages) {
        PowerQuestionPersonalityMapper mapper = getBaseMapper();
        QueryWrapper<PowerQuestionPersonality> wrapper = new QueryWrapper<>();
        IPage<PowerQuestionPersonality> page = new Page<>(pages.getCurrentPage(), pages.getPageSize());
        String desc = "desc";
        for (Map.Entry<String, Object> entry : pages.getParams().entrySet()) {
            if (!StringUtils.isNullOrEmpty(entry.getValue())){
                if (!"questionContent".equals(entry.getKey())){
                    wrapper.eq(StringUtils.upperCharToUnderLine(entry.getKey()),entry.getValue());
                }else{
                    wrapper.like("question_content","%"+entry.getValue()+"%");
                }
            }
        }
        if (!StringUtils.isNullOrEmpty(pages.getSortColumn())){
            if (desc.equals(pages.getSortMethod())){
                wrapper.orderByDesc(StringUtils.upperCharToUnderLine(pages.getSortColumn()));
            }else{
                wrapper.orderByAsc(StringUtils.upperCharToUnderLine(pages.getSortColumn()));
            }
        }
        IPage<PowerQuestionPersonality> selectPage = mapper.selectPage(page, wrapper);
        pages.setTotalPage((int) selectPage.getPages());
        pages.setTotalCount((int) selectPage.getTotal());
        List<PowerQuestionPersonality> records = selectPage.getRecords();
        List<QuestionPersonalityDto<String>> dtoList = new ArrayList<>();
        for (PowerQuestionPersonality common : records) {
            QuestionPersonalityDto<String> qc = new QuestionPersonalityDto<>();
            BeanUtils.copyProperties(common,qc);
            qc.setQuestionMediaUrl(JSON.parseArray(common.getQuestionMediaUrl(),String.class));
            qc.setQuestionOptionScore(JSON.parseArray(common.getQuestionOptionScore(),String.class));
            qc.setQuestionOptions(JSON.parseArray(common.getQuestionOptions(),String.class));
            qc.setQuestionMediaName(JSON.parseArray(common.getQuestionMediaName(),String.class));
            dtoList.add(qc);
        }
        pages.setList(dtoList);
        return pages;
    }

    @Override
    public List<QuestionCommonDto> getRandomList(PersonalityRandomVo vo) {
        PowerQuestionPersonalityMapper mapper = getBaseMapper();
        List<QuestionCommonDto> dtoList = new ArrayList<>();
        QueryWrapper<PowerQuestionPersonality> wrapper = new QueryWrapper<>();
        wrapper.eq("jobs_id",vo.getJobId());
        wrapper.eq("status",0);
        wrapper.eq("dimension_id",vo.getDimensionId());
        List<PowerQuestionPersonality> commons = mapper.selectList(wrapper);
        int i2 = commons.size();
        if (i2!=0){
            Set<Integer> random = StringUtils.random(commons.size(), 20);
            for (Integer index : random) {
                PowerQuestionPersonality common = commons.get(index);
                QuestionCommonDto<String> dto = new QuestionCommonDto<>();
                BeanUtils.copyProperties(common,dto);
                dto.setQuestionOptionScore(JSON.parseArray(common.getQuestionOptionScore(),String.class));
                dto.setQuestionMediaUrl(JSON.parseArray(common.getQuestionMediaUrl(),String.class));
                dto.setQuestionMediaName(JSON.parseArray(common.getQuestionMediaName(),String.class));
                dto.setQuestionOptions(JSON.parseArray(common.getQuestionOptions(),String.class));
                dtoList.add(dto);
            }
        }
        return dtoList;
    }

    private String mapToJson(String options){
        List<String> optList = Arrays.asList(options.split("，"));
        return JSON.toJSONString(optList);
    }
}
